﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XboxShmup
{

    public class SpaceShipEnemyTypeA:Enemy
    {
        const int ENEMY_HEIGHT= 61;
        const int ENEMY_WIDTH = 47;
        int health = 100;
        Vector2 shootingOffset;
        double RateOfFire = 3f;
        double timeToFireAgain = 0;
        SpriteAnimation movementAnimation = new SpriteAnimation()
        {
            blooping = true,
            columns = 8,
            rows = 1,
            drawPosition = Vector2.Zero,
            frameHeight = 61,
            frameWidth = 47,
            totalFrames = 8
        };
        SpriteAnimation explosionAnimation = new SpriteAnimation()
        {
            blooping = false,
            columns = 12,
            rows= 1,
            drawPosition = Vector2.Zero,
            frameHeight = 134,
            frameWidth= 133, 
            totalFrames = 12
        };


        public SpaceShipEnemyTypeA(Game1 gameClass, int xPosition, int yPosition)
            : base(gameClass, xPosition, yPosition, ENEMY_HEIGHT, ENEMY_WIDTH)
        {
            movementSpeed = 50;
            currentAnimation = movementAnimation;
            LoadContent();
            shootingOffset = new Vector2(0, (float)ENEMY_HEIGHT / 2f);
        }

        public override void LoadContent()
        {
            texture = gameClass.Content.Load<Texture2D>("mineAnimation");
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            timeToFireAgain -= gameTime.ElapsedGameTime.TotalSeconds;
            if (timeToFireAgain <= 0)
            {
                timeToFireAgain = RateOfFire;
                ShootProjectile();
            }
            position.X -= movementSpeed *(float)gameTime.ElapsedGameTime.TotalSeconds;
            base.Update(gameTime);
            if (bDestroyed == true && currentFrame >= currentAnimation.totalFrames - 1)
            {
                bCleanUp = true;
            }
        }

        public override bool CheckCollisionWith(WorldObject collidingObject)
        {
            bool bCollision = base.CheckCollisionWith(collidingObject);
            if(bCollision)
                if (collidingObject.objectType == ObjectType.PlayerShip)
                {
                    BlowUpEnemy();
                }
            return bCollision;
        }

        public override void HitByPlayerProjectileType(WorldObject hitByObject)
        {
            
            health -= ((Projectile)hitByObject).damage;
            if (health <= 0)
            {
                gameClass.playerScore += scoreValue;
                BlowUpEnemy();

            }
            

            base.HitByPlayerProjectileType(hitByObject);
        }

        private void ShootProjectile()
        {
            gameClass.ShootEnemyProjectile(new Vector2(-1, 0), position + shootingOffset);
        }

        private void BlowUpEnemy()
        {
            explosionSound.Play(0.5f,0,0);
            bCollidable = false;
            texture = gameClass.Content.Load<Texture2D>("explosion");
            currentAnimation = explosionAnimation;
            
            bDestroyed = true;
        }
        
    }

}
